package com.nfw.demo.service.impl;
import com.nfw.demo.dao.UsersMapper;
import com.nfw.demo.entity.Users;
import com.nfw.demo.service.EmailService;
import com.nfw.demo.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "userService")
public class UserServiceImpl implements UserService {

    @Value("${com.mailUrl}")
    private String sendUrl;

    @Autowired
    private UsersMapper usersMapper ;

    private static Logger logger = Logger.getLogger(UserServiceImpl.class);

    @Autowired
    private EmailService emailService;

    @Override
    public Users login(Users users) {
        Users users1= usersMapper.selectUserAndPswd(users);
        return users1;
    }

    @Override
    public List<Users> finAllUsers() {
        List<Users> usersList = usersMapper.selectUUUURRR();
        return usersList ;
    }

    @Override
    public int selectIsName(Users users) {
        return usersMapper.selectIsName(users);
    }



    @Override
    public String selectPasswordByName(Users users) {
        return usersMapper.selectPasswordByName(users);
    }

    @Override
    public Users selectAllByName(String username) {
        return usersMapper.selectAllByName(username);
    }

    @Override
    public Users checkCode(String code) {
        return usersMapper.checkCode(code);
    }

    @Override
    public void updateUserState(Users users) {
        usersMapper.updateUserState(users);
    }

    @Override
    public void register(Users users) {
        usersMapper.insert(users);
      //  usersMapper.upAntiCode(users);
        //获取激活码
        String code = users.getActiCode();
        logger.info("ActiCode:"+code);
        //主题
        String subject = "来自kiwi的激活邮件";
        //user/checkCode?code=code(激活码)是我们点击邮件链接之后根据激活码查询用户，如果存在说明一致，将用户状态修改为“1”激活
        //上面的激活码发送到用户注册邮箱
        StringBuffer context = new StringBuffer();
        context.append("<h1>你好哇:"+users.getUserName()+"</h1>")
                .append("<h1>激活请点击：</h1><br/><h3><a href='"+sendUrl+"checkCode?code="+code+"'>Click Me</a></h3>");
        //发送激活邮件
        emailService.sendEmail(users.getUserEmail(),subject,context);
        logger.info("发送的邮箱为:"+users.getUserEmail());
    }

    @Override
    public int findId(Users users) { return usersMapper.findId(users); }

    @Override
    public void updatePassword(Users users) { usersMapper.updatePassword(users); }

}
